<!--
 * @Author: your name
 * @Date: 2022-03-28 20:17:00
 * @LastEditTime: 2024-03-30 14:01:19
 * @LastEditors: smallWhite
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /huizhou_website_admin/src/views/about_us/company_profilees/components/modal.vue
-->
<template>
  <el-dialog :title="title"
    :visible.sync="dialogVisible"
    width="860px"
    :before-close="handleClose">
    <el-form ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="110px"
      class="demo-ruleForm">
      <el-form-item
        label="上传图片"
        prop="img">
        <el-upload
          class="avatar-uploader"
          :action="action"
          :show-file-list="false"
          name="multipartFile"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img
            v-if="ruleForm.imgUrl"
            :src="ruleForm.imgUrl"
            class="avatar">
          <i v-else
            class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-input
          v-show="false"
          v-model="ruleForm.img"
          placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item
        label="集团简介"
        prop="sort">
        <!-- <Editor :clear-edit="clearEdit" :content="ruleForm.concat" /> -->
        <Editors ref="Editor"
          v-model="ruleForm.concat"
          :conact="ruleForm.concat"
          class="CKEditor"
          @change="callbackChangeEditor" />
      </el-form-item>

    </el-form>
    <span slot="footer"
      class="dialog-footer">
      <el-button
        @click="resetForm('ruleForm')">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { REQUEST_LIST } from '@/api/requestList'
import { HTTP_URI } from '@/api/baseUrl'
export default {
  data() {
    return {
      action: HTTP_URI.UPLOADIMG,
      dialogVisible: false,
      title: '',
      clearEdit: true,
      img: '',
      ruleForm: {
        img: '',
        type: 1,
        imgUrl: '',
        concat: ''
      },
      rules: {
        sort: [{ required: false, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  methods: {
    open(data) {
      this.clearEdit = true
      this.ruleForm = data
      this.ruleForm.imgUrl = 'http://img.csjs.hzsdjzyjsxx.com/' + data.img
      this.dialogVisible = true
      this.title = '编辑集团简介'
      console.log(this.ruleForm.imgUrl)
    },
    callbackChangeEditor(value) {
      this.ruleForm.concat = value
    },
    handleClose() {
      this.dialogVisible = false
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {}
      // this.clearEdit = true
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.img = res.data.fileName
      this.ruleForm.imgUrl = res.data.previewUrl + '/' + res.data.fileName
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 10

      if (!isJPG) {
        this.$message.error('图片只能是JPG或者PNG 格式')
      }
      if (!isLt2M) {
        this.$message.error('图片大小不能超过 10MB!')
      }
      return isJPG && isLt2M
    },

    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          console.log(this.ruleForm)
          REQUEST_LIST.REQUST_FCN('COMPANYUPDATE', this.ruleForm, true).then(res => {
            if (res.status === 200) {
              this.$emit('ok')
              this.$message.success('编辑成功！')
              this.handleClose()
            }
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.handleClose()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
